package com.amani.module.medical_supplies.rest;


import com.amani.module.medical_supplies.biz.PurchaseBiz;
import com.amani.module.medical_supplies.entity.PurchaseOrder;
import com.amani.module.medical_supplies.entity.PurchaseOrderDetail;
import com.amani.module.medical_supplies.util.PropertyUtil;
import com.amani.module.medical_supplies.vo.PurchaseVO;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.rest.BaseController;
import com.github.wxiaoqi.security.common.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 11:51
 */
@RestController
@RequestMapping("purchase")
public class PurchaseOrderController extends BaseController<PurchaseBiz,PurchaseOrder> {

    @RequestMapping(value = "addpurchase",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse addPurchase(@Validated @RequestBody PurchaseVO vo,BindingResult bindingResult){
        ObjectRestResponse response = new ObjectRestResponse();
        response.setStatus(0);
        response.setMessage("增加失败");
        try {
            if (bindingResult.hasErrors()) {
                response.setMessage(bindingResult.getFieldError().getDefaultMessage());
                response.setData(vo);
                return response;
            }
            baseBiz.addPurchase(vo,response);
            response.setMessage("增加成功");
            response.setStatus(1);
        } catch (Exception e) {
            throw new BaseException("增加失败："+e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "getpurchase/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<PurchaseVO> getPurchase(@PathVariable int id){
        ObjectRestResponse<PurchaseVO> entityObjectRestResponse = new ObjectRestResponse<>();
        Object o = baseBiz.selPurchaseById(id);
        entityObjectRestResponse.data((PurchaseVO)o);
        return entityObjectRestResponse;
    }

    @RequestMapping(value = "commitApprovel/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse commitApprovel(@PathVariable int id){
        ObjectRestResponse response = new ObjectRestResponse<>();
        response.setStatus(0);
        response.setMessage("提交失败");
        //获取医耗采购审批流程图名称
        String bpmn = PropertyUtil.getProperty("medicalPurchaseBpmn");
        baseBiz.commitApprovelById(id,bpmn,response);
        return response;
    }

    @RequestMapping(value = "updatepurchase/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<PurchaseVO> updatePurchase(@Validated @RequestBody PurchaseVO vo,BindingResult bindingResult){
        ObjectRestResponse response = new ObjectRestResponse();
        response.setStatus(0);
        response.setMessage("更新失败");
        try {
            if(bindingResult.hasErrors()){
                response.setMessage(bindingResult.getFieldError().getDefaultMessage());
                response.setData(vo);
                return response;
            }
            baseBiz.updatePurchaseById(vo,response);
            response.setStatus(1);
            response.setMessage("更新成功");
        } catch (Exception e) {
            throw new BaseException("更新失败："+e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "pageApproval",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<PurchaseVO> pageApproval(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        //BaseContextHandler.setUserID("1");
        String processDefiniteKey = PropertyUtil.getProperty("medicalPurchaseBpmn");
        return baseBiz.selectpageApprovalByQuery(query,processDefiniteKey);
    }




}
